<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.File"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
<title>Scholar Aide</title>
<script language="javascript" type="text/javascript">
	function rateClicked(app_id, sch_id) {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "get");
		myForm.setAttribute('action', "validate_rate.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "application_id");
		data.setAttribute('value', app_id);
		myForm.appendChild(data);
		var data2 = document.createElement("input");
		data2.setAttribute('type', "hidden");
		data2.setAttribute('name', "scholarship_id");
		data2.setAttribute('value', sch_id);
		myForm.appendChild(data2);
		var data3 = document.createElement("input");
		data3.setAttribute('type', "hidden");
		data3.setAttribute('name', "rating");
		data3.setAttribute('value', ratings.elements[app_id + "name"].value);
		myForm.appendChild(data3);
		javascript: myForm.submit();
	}
	function redirectClicked(string) {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "post");
		myForm.setAttribute('action', "dashboard.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "file");
		data.setAttribute('value', string);
		myForm.appendChild(data);
		javascript: myForm.submit();
	}
</script>
</head>
<body>
	<jsp:include page="/includes/header.jsp" />
	<div id="wrapper">
		<div id="content">
			<table id="controls">
				<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('home.jsp')">Home</a></td>
				</tr>
				<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('edit_profile.jsp')">Edit Profile</a></td>
				</tr>
				<%
					// Retrieve submitted data.
					String scholarship_id = request.getParameter("scholarship_id");

					// Retrieve session variables.
					String type = (String) session.getAttribute("type");
					String user = (String) session.getAttribute("user");
					String errors = (String) session.getAttribute("errors");

					if (errors == null) {
						errors = "";
					}

					// Clear variables after reading them.
					session.setAttribute("errors", null);

					if (type.equalsIgnoreCase("student")) {
				%>
				<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('edit_profile.jsp')">View My Applications</a></td>
				</tr>
				<%
					} else if (type.equalsIgnoreCase("reviewer")) {
				%>
				<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('new_scholarship.jsp')">Create New Scholarship</a></td>
				</tr>
				<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('list_unfinished_scholarships.jsp')">Finish New Scholarship</a></td>
				</tr>
				<!--<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('review_applications.jsp')">Review Applications</a></td>
				</tr>-->
				<%
					}
				%>
				<tr>
					<td><a href="logout.jsp">Logout</a></td>
				</tr>
			</table>
			<form id="ratings">
				<table>
					<tr>
						<td>
						<div style="color: red; font-weight: bold; text-align: left;"><%=errors%></div>
						<%
							// Getting information and updating the database.
							java.sql.Connection conn = null;
							try {
								String myJDBCDriver = (String) session.getAttribute("myJDBCDriver");
								String myURL = (String) session.getAttribute("myURL");
								String myUserID = (String) session.getAttribute("myUserID");
								String myPassword = (String) session.getAttribute("myPassword");

								// Connect to the database.
								Class.forName(myJDBCDriver).newInstance();
								java.util.Properties sysprops = System.getProperties();
								sysprops.put("user", myUserID);
								sysprops.put("password", myPassword);
								conn = java.sql.DriverManager.getConnection(myURL, sysprops);

								java.sql.Statement statement = conn.createStatement();
								// Print the scholarship information.
								java.sql.ResultSet rs = statement.executeQuery("SELECT * FROM scholarships WHERE id='" + scholarship_id + "'");
								java.sql.ResultSetMetaData columns = rs.getMetaData();
								while (rs.next()) {
									out.println("<h1>Applications for the " + rs.getString("name") + "</h1>");
									if (rs.getInt("finished") == 1) {
										out.println("<table id=\"scholarship\">");
										for (int i = 2; i <= columns.getColumnCount() - 2; i++) {
											out.println("<tr>");
											out.println("<th>" + columns.getColumnName(i) + ":</th>");
											out.println("<td>" + rs.getString(i) + "</td>");
											out.println("</tr>");
										}
										String docPath = application.getRealPath("scholarship_documents") + "\\" + rs.getString("id") + ".doc";
										File file = new File(docPath);
										if (file.exists()) {
											out.println("<tr><th>extra_info</th><td><a href=\"" + "scholarship_documents/" + file.getName()
													+ "\">Download Extra Information</a></td></tr>");
										}
										out.println("</table>");
										out.println("<br />");
									}
								}
								// Print the applications.
								rs = statement
										.executeQuery("SELECT users.*, date_submitted, applications.id, rating FROM users JOIN applications ON username=applicant WHERE scholarship_id = '"
												+ scholarship_id + "'");
								columns = rs.getMetaData();
								out.println("<table id=\"scholarship\">");
								out.println("<tr>");
								for (int i = 5; i < columns.getColumnCount() - 1; i++) {
									out.println("<th style=\"border:1px solid black; text-align:left;\">" + columns.getColumnName(i) + "</th>");
								}
								out.println("<th style=\"border:1px solid black; text-align:left;\">documents</th>");
								out.println("<th style=\"border:1px solid black; text-align:left;\">rate</th>");
								out.println("</tr>");
								while (rs.next()) {
									out.println("<tr>");
									for (int i = 5; i < columns.getColumnCount() - 1; i++) {
										out.println("<td>" + rs.getString(i) + "</td>");
									}
									String docPath = application.getRealPath("application_documents") + "\\" + rs.getString("id") + ".doc";
									File file = new File(docPath);
									if (file.exists()) {
										out.println("<td><a href=\"application_documents/" + file.getName() + "\">Download</a></td>");
									} else {
										out.println("<td>None</td>");
									}
									out.println("<td><input type=\"text\" size=\"6\" value=\"" + rs.getString("rating") + "\"" + " id=\"" + rs.getString("id")
											+ "name\"><a href=\"Javascript:void();\" class=\"red_button\" onclick=\"rateClicked(" + rs.getString("id") + ","
											+ scholarship_id + ")\">Rate</a></td>");
									out.println("</tr>");
								}
								out.println("</table>");
							} catch (Exception e) {
								e.printStackTrace();
								out.print(e.toString());
							} finally {
								try {
									conn.close();
								} catch (Exception ee) {
								}
							}
						%>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<jsp:include page="/includes/footer.jsp" />
</body>
</html>